package com.gurun.codes.business.curtain;

import android.content.Context;

import com.gurun.codes.App;
import com.gurun.codes.R;
import com.gurun.codes.contract.base.BasePresenter;
import com.gurun.codes.contract.base.view.RxView;
import com.gurun.codes.utils.Constants;
import com.gurun.model.base.ResultObserver;
import com.gurun.model.base.RetrofitManager;
import com.gurun.model.bean.ApiResult;
import com.gurun.model.bean.curtain.CurtainControlRequestBean;
import com.gurun.model.service.CurtainService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface CurtainActivityContract {

    interface CurtainActivityView extends RxView {

        void onLoading();

        void onLoaded(ApiResult datas,Integer command);

        void onError(String msg);

        void onComplete();
    }

    class CurtainActivityViewPresenter extends BasePresenter<CurtainActivityView> {

        private Context mContext;

        public CurtainActivityViewPresenter(Context context) {
            this.mContext = context;
        }

        public void controlDevice(long deviceId, final Integer command, Integer curtainLevel, Integer cLearPos, Integer setPolarity) {
            CurtainControlRequestBean bean = new CurtainControlRequestBean();
            bean.setVersion(Constants.HTTP_VERSION);
            bean.setToken(App.getInstance().getLoginToken());
            bean.setUserId(App.getInstance().getLoginUserId());
            bean.setDeviceId(deviceId);
            if (command != null) {
                bean.setCommand(command);
            }
            if (curtainLevel != null) {
                bean.setCurtainLevel(curtainLevel);
            }
            if (cLearPos != null) {
                bean.setcLearPos(cLearPos);
            }
            if (setPolarity != null) {
                bean.setSetPolarity(setPolarity);
            }

            RetrofitManager.getInstance().createApiService(CurtainService.class)
                    .curtainControl(bean)
                    .compose(getView().<ApiResult>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new ResultObserver<ApiResult>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_connect_server_failed));
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }

                        @Override
                        public void onSuccess(ApiResult result, String msg) {
                            getView().onLoaded(result,command);
                        }

                        @Override
                        public void onApiError(ApiResult result) {
                            if (result != null) {
                                getView().onError(result.getRetMsg());
                            } else {
                                getView().onError(mContext.getString(R.string.common_connect_server_failed));
                            }
                        }
                    }));
        }
    }
}
